package com.tang.usercenter.controller;

import com.tang.common.config.shiroConfig.JwtToken;
import com.tang.common.utils.JwtUtils;
import com.tang.common.utils.UserUtils;
import com.tang.common.vo.R;
import com.tang.usercenter.entity.UserEntity;
import com.tang.usercenter.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public R register(@RequestBody UserEntity entity){
        try {
            userService.register(entity);
            return R.success();
        }catch (RuntimeException e){
            log.error(e.getMessage(),e);
            return R.error(e.getLocalizedMessage());
        }
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody UserEntity entity){
        try {
            return R.success(userService.login(entity));
        }catch (UnknownAccountException e){
            return R.error("账号不存在");
        }catch (IncorrectCredentialsException e){
            return R.error("密码不正确");
        }
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    public R logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return R.success();
    }

    @ApiOperation("通过id获取用户")
    @GetMapping("/getById/{id}")
    public R<UserEntity> getById(@PathVariable Long id){
        try {
            return R.success(userService.getById(id));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("通过用户名获取用户")
    @GetMapping("/getByUserName/{userName}")
    public R<UserEntity> getByUserName(@PathVariable String userName){
        try {
            return R.success(userService.getUserByUserName(userName));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("认证")
    @GetMapping("/auth")
    public R auth(@RequestParam String token){
        try {
            log.info("开始认证：{}",token);
            SecurityUtils.getSubject().login(new JwtToken(token));
            return R.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("获取当前用户")
    @GetMapping("/getCurrentUser")
    public R<UserEntity> getCurrentUser(){
        try {
            return R.success(userService.getCurrentUser());
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return R.error(e.getMessage());
        }

    }
}
